package com.chungechunyemen.post.action;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.chungechunyemen.post.po.Postbar;
import com.chungechunyemen.post.po.Replies;
import com.chungechunyemen.post.po.Topic;
import com.chungechunyemen.post.services.IPostService;
import com.chungechunyemen.util.Constants;
import com.chungechunyemen.util.PageInfo;
import com.chungechunyemen.util.StringUtils;
import com.opensymphony.xwork2.ActionSupport;

public class PostAction extends ActionSupport {
	private static final long serialVersionUID = -7518580100312529970L;
	private Postbar postbar = new Postbar();
	private Topic topic = new Topic();
	private Replies replies = new Replies();
	private List topicList = new ArrayList();
	private List repliesList = new ArrayList();
	// 分页信息
	private PageInfo pageInfo = new PageInfo();
	// 验证码
	private String validateCode;
	// return messages
	private String tip = "";
	private String returnUrl = "";

	// Services
	private IPostService postService;

	// 发贴
	public String postTopic() {
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		topic.setViews(0);
		topic.setReplies(1);
		topic.setIstop(0);
		topic.setHot(0);
		topic.setUserIporun(1);
		topic.setLastuserIporun(1);
		topic.setState(1);
		// 设置作者IP
		topic.setUserIp(ServletActionContext.getRequest().getRemoteAddr());
		// 最后回复时间发贴时间
		topic.setLasttime(timestamp);
		// 设置回复人IP
		replies.setIp(ServletActionContext.getRequest().getRemoteAddr());
		// 设置回复时间
		replies.setDatetime(timestamp);
		replies.setPosition(1);
		replies.setIporun(1);
		replies.setState(1);
		// 设置所属贴吧
		topic.setPostbar(postbar);
		// 提示信息与返回地址
		tip = "发贴成功!ID：" + postService.postTopic(topic, replies);
		// 设置返回地址
		returnUrl = ServletActionContext.getRequest().getHeader("Referer");
		return SUCCESS;
	}

	// 回复
	public String postReply() {
		// 查询这个贴子
		topic = postService.getTopic(topic.getTopicId());
		if (topic == null) {
			tip = "您要浏览的贴子不存在!";
			returnUrl = ServletActionContext.getRequest().getHeader("Referer");
			return INPUT;
		}
		// 获取当前时间
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());
		// 设置所属贴子
		replies.setTopic(topic);
		// 设置回复人IP
		replies.setIp(ServletActionContext.getRequest().getRemoteAddr());
		// 设置回复时间
		replies.setDatetime(timestamp);
		replies.setPosition(1);
		replies.setIporun(1);
		replies.setState(1);

		postService.postReply(replies);

		// 设置返回地址
		returnUrl = ServletActionContext.getRequest().getHeader("Referer");
		return SUCCESS;
	}

	// 浏览某个贴吧的贴子列表
	public String viewTopicList() {
		// 设置贴吧名称
		if (postbar.getSubject() == null || postbar.getSubject().equals("")) {
			postbar.setSubject("春哥纯爷们");
		} else {
			try {
				postbar.setSubject(URLDecoder.decode(postbar.getSubject(),
						"UTF-8"));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		// 查询这个贴吧
		postbar = postService.getPostbar(postbar.getSubject());
		if (postbar == null) {
			tip = "没有这个贴吧!";
			returnUrl = ServletActionContext.getRequest().getHeader("Referer");
			return INPUT;
		}

		// 分页信息
		if (pageInfo.getAmountInPage() <= 0) {
			// 默认每页显示n个贴子
			pageInfo.setAmountInPage(Constants.MAXIMUM_TOPICS_IN_PAGE);
		}
		pageInfo.setCountRows(postService.getTopicsCount(postbar.getSubject()));
		pageInfo.setCountPages((pageInfo.getCountRows()
				+ pageInfo.getAmountInPage() - 1)
				/ pageInfo.getAmountInPage());

		// 查询贴子列表
		topicList = postService.viewTopicList(postbar.getSubject(), (pageInfo
				.getCurrentPage() - 1)
				* pageInfo.getAmountInPage(), pageInfo.getAmountInPage());
		return SUCCESS;
	}

	// 浏览某个贴子的回复列表
	public String viewRepliesList() {
		// 设置贴子ID
		if (topic.getTopicId() == null || topic.getTopicId() == 0) {
			return INPUT;
		}

		// 查询这个贴子
		topic = postService.getTopic(topic.getTopicId());
		if (topic == null) {
			tip = "您要浏览的贴子不存在!";
			returnUrl = ServletActionContext.getRequest().getHeader("Referer");
			return INPUT;
		}

		// 浏览数+1
		postService.increaseTopicViews(topic.getTopicId());

		// 查询贴吧信息
		postbar = postService.getPostbar(topic.getPostbar().getPostbarId());

		// 分页信息
		if (pageInfo.getAmountInPage() <= 0) {
			// 默认每页显示n个回复
			pageInfo.setAmountInPage(Constants.MAXIMUM_REPLIES_IN_PAGE);
		}
		pageInfo.setCountRows(postService.getRepliesCount(topic.getTopicId()));
		pageInfo.setCountPages((pageInfo.getCountRows()
				+ pageInfo.getAmountInPage() - 1)
				/ pageInfo.getAmountInPage());

		// 查询回复列表
		repliesList = postService.viewRepliesList(topic.getTopicId(), (pageInfo
				.getCurrentPage() - 1)
				* pageInfo.getAmountInPage(), pageInfo.getAmountInPage());
		return SUCCESS;
	}

	public IPostService getPostService() {
		return postService;
	}

	public void setPostService(IPostService postService) {
		this.postService = postService;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public String getValidateCode() {
		return validateCode;
	}

	public void setValidateCode(String validateCode) {
		this.validateCode = validateCode;
	}

	public String getTip() {
		return tip;
	}

	public void setTip(String tip) {
		this.tip = tip;
	}

	public List getTopicList() {
		return topicList;
	}

	public void setTopicList(List topicList) {
		this.topicList = topicList;
	}

	public Postbar getPostbar() {
		return postbar;
	}

	public void setPostbar(Postbar postbar) {
		this.postbar = postbar;
	}

	public Replies getReplies() {
		return replies;
	}

	public void setReplies(Replies replies) {
		this.replies = replies;
	}

	public List getRepliesList() {
		return repliesList;
	}

	public void setRepliesList(List repliesList) {
		this.repliesList = repliesList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

}
